import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2

Window {
    id: appLogin
    visible: true
    maximumWidth: 487
    maximumHeight: 262
    minimumHeight: 262
    minimumWidth: 487
    title: qsTr("雁联风控管理端登录")
    property var bkUrl: "qrc:/Res/image/bk_login.bmp"
    property int m: 0
    Image {
        id: bkImg
        anchors.fill: parent
        source: bkUrl //"qrc:/Res/image/bk_login.bmp"
    }

    ColumnLayout {
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        height: parent.height / 2
        width: parent.width / 2
        anchors.rightMargin: 20
        anchors.bottomMargin: 18

        RowLayout {
            width: parent.width
            Text {
                text: "用户名"
                horizontalAlignment: Text.AlignRight
                Layout.fillWidth: true
            }

            TextField {
                id: userName
                width: 100
                //                validator: IntValidator {
                //                    bottom: 0
                //                    top: 1999999999
                //                }
                //                inputMethodHints: Qt.ImhDigitsOnly
                //                maximumLength:11;
                text: config.getLastUserID()
            }
        }
        RowLayout {
            width: parent.width
            Text {
                text: "登录密码"
                horizontalAlignment: Text.AlignRight
                Layout.fillWidth: true
            }

            TextField {
                id: userPwd
                echoMode: TextInput.Password
                width: 100
                //                text: config.getLastUserPWD()
                Component.objectName: {
                    var sPwd = config.getLastUserPWD()
                    console.log("userPwd:" + sPwd)
                    if (sPwd === "" || sPwd === null) {
                        savePwd.checked = false
                    } else {
                        savePwd.checked = true
                        userPwd.text = sPwd
                    }
                }
            }
        }

        CheckBox {
            id: savePwd
            text: "记住密码"
        }

        RowLayout {
            width: 180
            Layout.alignment: Qt.AlignRight
            spacing: 20
            Button {
                text: "登录"
                focus: true
                onClicked: {
                    //                    var m = Math.random() * 10 % 3
                    //                    m++
                    //                    if (0 == m % 3)
                    //                        bkUrl = "qrc:/Res/image/bk_login.bmp"
                    //                    else if (1 == m % 3)
                    //                        bkUrl = "qrc:/Res/image/SearchPic.png"
                    //                    else if (2 == m % 3)
                    //                        bkUrl = "qrc:/Res/image/cx_cp.bmp"
                    //                    console.log("change bk " + bkUrl)
                    //                    return
                    var nLogRet = g_MainQuo.doLogin(userName.text, userPwd.text)
                    if (0 === nLogRet) {
                        if (savePwd.checked)
                            config.saveUser(userName.text, userPwd.text)
                        else
                            config.saveUser(userName.text, "")

                        appLogin.close()
                    } else
                        tipText.text = '登录失败 ' + g_MainQuo.getPopMsg()
                }
            }

            Button {
                text: "退出"
                onClicked: {
                    appLogin.close()
                }
            }
        }
    }

    Text {
        id: tipText
        width: 300
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        anchors.left: parent.left
        color: "red"
    }

    function openRiskWatch() {
        console.log("login window")
    }
}
